﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Windows.Media;
using System.Collections;
using AgilePlannerDT.Data;

namespace AgilePlannerDT.Utils
{
    /// <summary>
    /// Converts between the set of predefined index card colors and their names
    /// as well as between colors and brushes.
    /// </summary>
    public class CardColorConverter : TypeConverter
    {
        private List<Color> values = new List<Color>() { CardConstants.AQUA, CardConstants.BLUE, CardConstants.GRAY,
            CardConstants.GREEN, CardConstants.KHAKI, CardConstants.PEACH, CardConstants.PINK,
            CardConstants.RED, CardConstants.WHITE, CardConstants.YELLOW,CardConstants.TRANSPARENT};

        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == typeof(string) || sourceType == typeof(Brush))
            {
                return true;
            }

            return base.CanConvertFrom(context, sourceType);
        }

        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if (destinationType == typeof(Color))
            {
                return true;
            }

            return base.CanConvertTo(context, destinationType);
        }

        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            if (value is string)
            {
                string color = ((string)value).ToLower();

                if (color.Equals(CardConstants.CardColors.aqua.ToString()))
                    return CardConstants.AQUA;
                else if (color.Equals(CardConstants.CardColors.blue.ToString()))
                    return CardConstants.BLUE;
                else if (color.Equals(CardConstants.CardColors.gray.ToString()))
                    return CardConstants.GRAY;
                else if (color.Equals(CardConstants.CardColors.green.ToString()))
                    return CardConstants.GREEN;
                else if (color.Equals(CardConstants.CardColors.khaki.ToString()))
                    return CardConstants.KHAKI;
                else if (color.Equals(CardConstants.CardColors.peach.ToString()))
                    return CardConstants.PEACH;
                else if (color.Equals(CardConstants.CardColors.pink.ToString()))
                    return CardConstants.PINK;
                else if (color.Equals(CardConstants.CardColors.red.ToString()))
                    return CardConstants.RED;
                else if (color.Equals(CardConstants.CardColors.white.ToString()))
                    return CardConstants.WHITE;
                else if (color.Equals(CardConstants.CardColors.yellow.ToString()))
                    return CardConstants.YELLOW;
                else if (color.Equals(CardConstants.CardColors.transparent.ToString()))
                    return CardConstants.TRANSPARENT;
            }
            else if (value is Brush)
            {
                Brush brush = (Brush)value;

                BrushConverter bc = new BrushConverter();

                String clr = (String)bc.ConvertTo(brush, typeof(string));

                ColorConverter cc = new ColorConverter();

                return (Color)cc.ConvertFrom(clr);                 
            }

            return base.ConvertFrom(context, culture, value);
        }

        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                if (value is Color)
                {
                    Color color = (Color)value;

                    if (color.Equals(CardConstants.AQUA))
                        return CardConstants.CardColors.aqua.ToString();
                    else if (color.Equals(CardConstants.BLUE))
                        return CardConstants.CardColors.blue.ToString();
                    else if (color.Equals(CardConstants.GRAY))
                        return CardConstants.CardColors.gray.ToString();
                    else if (color.Equals(CardConstants.GREEN))
                        return CardConstants.CardColors.green.ToString();
                    else if (color.Equals(CardConstants.KHAKI))
                        return CardConstants.CardColors.khaki.ToString();
                    else if (color.Equals(CardConstants.PEACH))
                        return CardConstants.CardColors.peach.ToString();
                    else if (color.Equals(CardConstants.PINK))
                        return CardConstants.CardColors.pink.ToString();
                    else if (color.Equals(CardConstants.RED))
                        return CardConstants.CardColors.red.ToString();
                    else if (color.Equals(CardConstants.WHITE))
                        return CardConstants.CardColors.white.ToString();
                    else if (color.Equals(CardConstants.YELLOW))
                        return CardConstants.CardColors.yellow.ToString();
                    else if (color.Equals(CardConstants.TRANSPARENT))
                        return CardConstants.CardColors.transparent.ToString();
                }
            }

            return base.ConvertTo(context, culture, value, destinationType);
        }

        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            return true;
        }

        public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            return new StandardValuesCollection(values);
        }
    }
}